package com.starchen.optimalselection.admin.controller;

import com.starchen.optimalselection.admin.model.AdminMenu;
import com.starchen.optimalselection.admin.model.form.AssginMenuForm;
import com.starchen.optimalselection.admin.service.AdminMenuService;
import com.starchen.optimalselection.admin.service.AdminRoleMenuService;
import com.starchen.optimalselection.common.utils.ResponseEnum;
import com.starchen.optimalselection.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author daxilong
 * @date 2025/3/21 21:25
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/menu")
public class AdminMenuController {

    @Autowired
    private AdminMenuService adminMenuService;

    @Autowired
    private AdminRoleMenuService adminRoleMenuService;

    @ApiOperation("菜单列表（树形）")
    @GetMapping("treelist")
    public Result findNodes() {
        List<AdminMenu> list = adminMenuService.findNodes();
        return Result.success(list);
    }

    @ApiOperation("添加菜单")
    @PostMapping("save")
    public Result save(@RequestBody AdminMenu sysMenu) {
        boolean success = adminMenuService.save(sysMenu);
        if(success)
            return Result.success();
        else
            return Result.error();
    }

    @ApiOperation("修改菜单")
    @PostMapping("update")
    public Result update(@RequestBody AdminMenu sysMenu) {
        boolean success = adminMenuService.updateById(sysMenu);
        if(success)
            return Result.success();
        else
            return Result.error();
    }

    @ApiOperation("根据id查询")
    @GetMapping("/info/{id}")
    public Result getUser(@PathVariable Long id) {
        AdminMenu menu = adminMenuService.getById(id);
        return Result.success(menu);
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("delete/{id}")
    public Result remove(@PathVariable Long id) {
        boolean success = adminMenuService.removeMenuById(id);
        if(success)
            return Result.success();
        else
            return Result.error(ResponseEnum.NODE_ERROR);
    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("/relation/{roleId}")
    public Result getMenu(@PathVariable Long roleId) {
        List<AdminMenu> list = adminRoleMenuService.findMenuByRoleId(roleId);
        return Result.success(list);
    }

    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/assign")
    public Result doAssign(@RequestBody AssginMenuForm assginMenuVo) {
        adminRoleMenuService.doAssign(assginMenuVo);
        return Result.success();
    }

}
